﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;
using WXYW.Model;

namespace WXYW
{
    public partial class ManageRolePermission : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindPermission();
            }
        }

        private void BindPermission()
        {
            UserService roleService = new UserService();
            UserType role = roleService.GetUserTypeByID(Convert.ToInt32(Request.QueryString[Constants.QUERYSTR_ROLE_ID]));
            RoleNameLbl.Text = role.Name;

            RoleService service = new RoleService();
            LCGLGridView.DataSource = service.GetRolePermissionByGroupID(Convert.ToInt32(Request.QueryString[Constants.QUERYSTR_ROLE_ID]),
                (int)MenuGroup.LCGL);
            LCGLGridView.DataBind();

            SJGridView.DataSource = service.GetRolePermissionByGroupID(Convert.ToInt32(Request.QueryString[Constants.QUERYSTR_ROLE_ID]),
                (int)MenuGroup.SJGL);
            SJGridView.DataBind();

            BJXXGridView.DataSource = service.GetRolePermissionByGroupID(Convert.ToInt32(Request.QueryString[Constants.QUERYSTR_ROLE_ID]),
                (int)MenuGroup.BJXX);
            BJXXGridView.DataBind();

            BJGridView.DataSource = service.GetRolePermissionByGroupID(Convert.ToInt32(Request.QueryString[Constants.QUERYSTR_ROLE_ID]),
                (int)MenuGroup.BJSQ);
            BJGridView.DataBind();

            FCGridView.DataSource = service.GetRolePermissionByGroupID(Convert.ToInt32(Request.QueryString[Constants.QUERYSTR_ROLE_ID]),
                (int)MenuGroup.FCSQ);
            FCGridView.DataBind();

            SJWHGridView.DataSource = service.GetRolePermissionByGroupID(Convert.ToInt32(Request.QueryString[Constants.QUERYSTR_ROLE_ID]),
                (int)MenuGroup.SJWH);
            SJWHGridView.DataBind();

            TJGridView.DataSource = service.GetRolePermissionByGroupID(Convert.ToInt32(Request.QueryString[Constants.QUERYSTR_ROLE_ID]),
                (int)MenuGroup.TJXX);
            TJGridView.DataBind();

            JCGridView.DataSource = service.GetRolePermissionByGroupID(Convert.ToInt32(Request.QueryString[Constants.QUERYSTR_ROLE_ID]),
                (int)MenuGroup.JCXX);
            JCGridView.DataBind();

            QZLCGridView.DataSource = service.GetRolePermissionByGroupID(Convert.ToInt32(Request.QueryString[Constants.QUERYSTR_ROLE_ID]),
                (int)MenuGroup.QZLC);
            QZLCGridView.DataBind();

            EPXXGridView.DataSource = service.GetRolePermissionByGroupID(Convert.ToInt32(Request.QueryString[Constants.QUERYSTR_ROLE_ID]), (int)MenuGroup.EPXX);
            EPXXGridView.DataBind();


        }

        protected void LCGLGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                ((Label)e.Row.FindControl("IDLbl")).Text = (e.Row.RowIndex + 1).ToString();
            }
        }

        protected void EPXXGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                ((Label)e.Row.FindControl("IDLbl")).Text = (e.Row.RowIndex + 1).ToString();
            }
        }

        protected void saveLnk_Click(object sender, EventArgs e)
        {
            UpdateRole(LCGLGridView.Rows);
            UpdateRole(SJGridView.Rows);
            UpdateRole(BJXXGridView.Rows);
            UpdateRole(BJGridView.Rows);
            UpdateRole(FCGridView.Rows);
            UpdateRole(SJWHGridView.Rows);
            UpdateRole(TJGridView.Rows);
            UpdateRole(JCGridView.Rows);
            UpdateRole(QZLCGridView.Rows);
            UpdateRole(EPXXGridView.Rows);
            Response.Write(string.Format("<script language='JavaScript'> alert( '权限配置更改已经保存');location.href= 'ManageRole.aspx' </script> "));
        }

        private void UpdateRole(GridViewRowCollection rows)
        {
            RoleService roleService = new RoleService();
            foreach (GridViewRow row in rows)
            {
                int menuID = Convert.ToInt32(((HiddenField)row.FindControl("MenuID")).Value);
                CheckBox viewCheckBox = (CheckBox)row.FindControl("ViewCheckBox");
                CheckBox opCheckBox = (CheckBox)row.FindControl("OPCheckBox");
                if (opCheckBox.Checked)
                {
                    roleService.UpdateRolePermission(Convert.ToInt32(Request.QueryString[Constants.QUERYSTR_ROLE_ID]), menuID, 2);
                }
                else if (viewCheckBox.Checked)
                {
                    roleService.UpdateRolePermission(Convert.ToInt32(Request.QueryString[Constants.QUERYSTR_ROLE_ID]), menuID, 1);
                }
                else
                {
                    roleService.UpdateRolePermission(Convert.ToInt32(Request.QueryString[Constants.QUERYSTR_ROLE_ID]), menuID, 0);
                }
            }
        }
    }
}
